#include "PhysicsCore.h"

COLLISION PhysicsCore::CheckCollision(D2D1_RECT_F boundingBox, D2D1_RECT_F objBoundingBox)
{
	if(boundingBox.top <= objBoundingBox.bottom && boundingBox.bottom >= objBoundingBox.top && boundingBox.left <= objBoundingBox.right && boundingBox.right >= objBoundingBox.left)
	{
		D2D1_RECT_F penetration;
		penetration.left = fabs(boundingBox.right-objBoundingBox.left);
		penetration.top = fabs(boundingBox.bottom-objBoundingBox.top);
		penetration.bottom = fabs(boundingBox.top-objBoundingBox.bottom);
		penetration.right = fabs(boundingBox.left - objBoundingBox.right);
		
		if(penetration.top < penetration.bottom && penetration.top < penetration.left && penetration.top < penetration.right)
		       return TOP_COLLISION;
		else if(penetration.bottom < penetration.top && penetration.bottom < penetration.left && penetration.bottom < penetration.right)
		      return BOTTOM_COLLISION;
		else if(penetration.left < penetration.right && penetration.left < penetration.top && penetration.left < penetration.bottom)
		       return LEFT_COLLISION;
		else
		       return RIGHT_COLLISION;
	}
	
	return NO_COLLISION;
}

D2D1_SIZE_F PhysicsCore::CheckCollision(D2D1_ELLIPSE obj1, D2D1_ELLIPSE obj2)
{
	D2D1_SIZE_F collisionPos;

	FLOAT distX = (obj1.point.x*obj1.point.x)-(obj2.point.x*obj2.point.x);
	FLOAT distY = (obj1.point.y*obj1.point.y)-(obj2.point.y*obj2.point.y);
	FLOAT radiusDist = fabs((obj1.radiusX*obj1.radiusX)+(obj2.radiusX*obj2.radiusX));

	if((fabs(distX)+fabs(distY))<=radiusDist)
	{
		collisionPos.height = distY/(distY+distX);
		collisionPos.width = distX/(distY+distX);
	}

	return collisionPos;
}